import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
import { component } from 'vue/types/umd'


Vue.use(VueRouter)

const routes: Array<RouteConfig> = [
  { path: '/', redirect: '/login' },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
  },

  {
    path: '/Home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome', component: () => import('../views/welcome.vue')
      },
      {
        path: '/user', component: () => import("../views/user.vue")
      },

      {
        path: '/role',
        component: () => import('../views/role.vue')
      },
      {
        name: 'userMessage',
        path: '/userMessage',
        component: () => import('../views/showUserMessage.vue')
      },

      {
        name: 'class',
        path: "/class",
        component: () => import("../views/class.vue")
      },
      {
        name: "department",
        path: "/department",
        component: () => import("../views/department.vue")
      },
      {
        name: "school",
        path: "/school",
        component: () => import("../views/school.vue")
      },
      {
        name: "teacherManager",
        path: "/teacherManager",
        component: () => import("../views/teacherManager.vue")
      },
      {
        name: "questionsType",
        path: "/questionsType",
        component: () => import("../views/questionsType.vue")
      },
      {
        name: "questionsDetail",
        path: "/questionsDetail",
        component: () => import("../views/questionsDetail.vue")
      },
      {
        name: "questionsAddPage",
        path: "/questionsAddPage",
        component: () => import("../views/questionAddPage.vue")
      },
      {
        name: "exams",
        path: "/exams",
        component: () => import("../views/exams.vue")
      },
      {
        name: "examAddOrUpdate",
        path: "/examAddOrUpdate",
        component: () => import("../views/examAddOrUpdate.vue")
      },
      {
        name: "active",
        path: "/active",
        component: () => import("../views/active.vue")
      },
      {
        name: "activeAddOrUpdate",
        path: "/activeAddOrUpdate",
        component: () => import("../views/activeAddOrUpdate.vue")
      },
      {
        name: "examAssignStudent",
        path: "/examAssignStudent",
        component: () => import("../views/examAssignStudent.vue")
      },
      {
        name: "examAssignClass",
        path: "/examAssignClass",
        component: () => import("../views/examAssignClass.vue")
      },
      {
        name: "examCheckDetail",
        path: "/examCheckDetail",
        component: () => import("../views/examCheckDetail.vue")
      },
      {
        name: "activeAssignStudent",
        path: "/activeAssignStudent",
        component: () => import("../views/activeAssignStudent.vue")
      },

      {
        name: "activeAssignClass",
        path: "/activeAssignClass",
        component: () => import("../views/activeAssignClass.vue")
      },
      {
        name: "activeCheckDetail",
        path: "/activeCheckDetail/:id",
        component: () => import("../views/activeCheckDetail.vue")
      }
    ]
  },
  {
    path: "/showCode",
    name: "showCode",
    component: () => import('../views/showCode.vue')
  },
  {
    path: "/404",
    name: "NotFound",
    component: () => import('../views/NotFind.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: "/404"
  },


]

const router = new VueRouter({
  routes
})
// to:即将要进入的目标路由对象
// from:当前导航正要离开的路由
// next:在前置导航守卫中，一定要写next(),否则页面不会跳转页面
// 路由守卫 判断token 是否失效
router.beforeEach((to, from, next) => {
  // 如果接下来要去/login  拿到localstroge的token
  if (to.path === '/login') {
    const tokenStr = window.localStorage.getItem('authorization')
    if (tokenStr) {
      console.log("token", tokenStr)
      return next('/welcome')
    } else {
      return next()
    };

  } //你要访问登录页面 我们直接放行
  // 获取token 我们可以调用
  //如果token 不存在 强制跳转到 token
  next() //如果有的话 就放行
})

export default router
